﻿using Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DAL
{
    public class D_StuChoice
    {
        public List<StuChoice> getList()
        {
            var list = SqlSugarHelper.Db.Queryable<StuChoice>().ToList();
            return list;
        }

        public bool saveResult(List<StuChoice> gs)
        {
            var count = SqlSugarHelper.Db.Storageable<StuChoice>(gs).ExecuteCommand();
            return count == gs.Count ? true : false;
        }

        public int commit(StuChoice model)
        {
            var count = SqlSugarHelper.Db.Storageable(model).ExecuteCommand();
            return count;
        }

        public StuChoice select(Models.Student student)
        {
            var m_g = SqlSugarHelper.Db.Queryable<StuChoice>().Where(g => g.Group_id == student.group_id).ToList();
            if (m_g.Count > 0)
                return m_g[0];
            else return null;
        }

        public bool del(StuGroup stu_Group)
        {
            var res = SqlSugarHelper.Db.Deleteable<StuChoice>().Where(g => g.Group_id == stu_Group.group_id).ExecuteCommand();
            return res == 1 ? true : false;
        }

        public List<StuChoice> GetList()
        {

             var list = SqlSugarHelper.Db.Queryable<StuChoice, StuGroup, Teacher, Teacher, Teacher>
                ((g, sg, t1, t2, t3) => new JoinQueryInfos(
                JoinType.Left, g.Group_id == sg.group_id,
                JoinType.Left, g.Choice_1 == t1.teacher_id,
                JoinType.Left, g.Choice_2 == t2.teacher_id,
                JoinType.Left, g.Choice_3 == t3.teacher_id
                ))
                .Select((g, sg, t1, t2, t3) => new StuChoice
                {
                    Group_id = g.Group_id,
                    group_name = sg.group_name,
                    project_name = sg.project_name,
                    Teacher_1 = t1.teacher_name,
                    Teacher_2 = t2.teacher_name,
                    Teacher_3 = t3.teacher_name,
                })
                .ToList();

            return list;
        }

    }
}
